import scrapy
from scrapy import Request, Selector

from bys.items import CampusItem


class CampusSpider(scrapy.Spider):
    """爬取招聘公告"""
    name = 'campus'
    allowed_domains = ['cqu.cqbys.com']

    def start_requests(self):
        for page in range(10):
            yield Request(url=f'https://cqu.cqbys.com/campus/index/domain/cqu/city//page/{page + 1}')

    def parse(self, response):
        sel = Selector(response)
        list_items = sel.css('ul.infoList')
        for list_item in list_items:
            detail_url = list_item.css('li.span7 > a::attr(href)').extract_first()
            campus_item = CampusItem()
            campus_item['title'] = list_item.css('li.span7 > a::text').extract_first()
            campus_item['date'] = list_item.css('li.span4::text').extract_first()
            yield campus_item
